<?php
/**********************************************************************************
WIKINDX: Bibliographic Management system.
Copyright (C)

This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program;
if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

The WIKINDX Team 2007
sirfragalot@users.sourceforge.net
**********************************************************************************/
/*****
* FRONT class
*
* Front page of the system.
*****/
class FRONT
{
// Constructor
	function FRONT($db)
	{
		$this->db = $db;
		include_once("core/messages/MESSAGES.php");
		$this->messages = new MESSAGES();
		include_once("core/template/TEMPLATE.php");
		$this->template = new TEMPLATE('content');
		include_once("core/session/SESSION.php");
		$session = new SESSION();
		$session->setVar('sql_front', TRUE); // for navigation after edit etc. operations.
		$this->message = FALSE;
	}
// Display front page information.  If $noMenu, display WIKINDX submenu links
	function display($noMenu = FALSE)
	{
		include_once("core/html/HTML.php");
		$html = new HTML();
		include_once("core/html/MISC.php");
		include_once("core/messages/MESSAGES.php");
		include_once("core/session/SESSION.php");
		$this->session = new SESSION();
		$this->template->setVar('heading', 'WIKINDX');
		$recordset = $this->db->select(array('WKX_config'), array("description", "contactEmail"));
		$row = $this->db->fetchRow($recordset);
		if($noMenu)
		{
			include_once("core/html/MENU.php");
			$menu = new MENU();
			$pString = $menu->noMenu('wikindx');
			$this->template->setVar('noMenu', $pString);
		}
		$pString = $this->message ? $this->message : FALSE;
		$pString .= $html->dbToHtmlTidy($row['description']);
		if($lastChanges = $this->session->getVar("setup_lastChanges"))
			$pString .= $this->getChanges($lastChanges);
		$this->template->setVar('body', $pString);
		if($row['contactEmail'])
		{
			$email = $html->dbToHtmlTidy($row['contactEmail']);
			$email = MISC::a("link", $email, "mailto:$email");
			$this->template->setVar('contact', $email);
		}
		return $this->template->process();
	}
// Get recently added/edited resources
	function getChanges($limit)
	{
// If no resources, return FALSE
		$resultSet = $this->db->select(array('WKX_resource'), array('id'));
		if(!$this->db->numRows($resultSet))
			return FALSE;
		include("config.php");
		include_once("core/statistics/STATISTICS.php");
		$stats = new STATISTICS($this->db, FALSE);
		include_once("core/sql/STATEMENTS.php");
		$stmt = new STATEMENTS($this->db);
		include_once("core/icons/LOADICONS.php");
		$icons = new LOADICONS();
		include_once("core/html/TABLE.php");
		include_once("core/styles/BIBSTYLE.php");
		$bibStyle = new BIBSTYLE($this->db, "html");
		include_once("core/user/USER.php");
		$user = new USER($this->db);
		include_once("core/file/export/COINS.php");
		$coins = new COINS($this->db);
		$write = $this->session->getVar('setup_write');
		$userId = $this->session->getVar('setup_userId');
		$superAdmin = $this->session->getVar('setup_superadmin');
		$multiUser = $this->session->getVar('setup_multiUser');
// type of CSS class required.
		$icons->getIconInfo('view');
		$icons->getIconInfo('viewmeta');
		$icons->getIconInfo('edit');
		$icons->getIconInfo('bibtex');
		$icons->getIconInfo('delete');
		$viewLink = $icons->viewExists ? "imgLink" : "link";
		$editLink = $icons->editExists ? "imgLink" : "link";
		$deleteLink = $icons->editExists ? "imgLink" : "link";
		$bibtexLink = $icons->bibtexExists ? "imgLink" : "link";
		$stmt->ascDesc = 'DESC';
		if($this->session->getVar("setup_lastChangesType") == 'D') // Display from last $limit days
		{
			$condition = " WHERE DATE_SUB(CURDATE(), INTERVAL $limit DAY) 
				<= " . $this->db->formatField('timestamp');
			if($limitResources = $this->session->getVar("setup_lastChangesDayLimit"))
			{
				$sqlTotal = $this->db->selectNoExecute(array("WKX_resource"),  
					$stmt->listFields('timestamp'), 
					$stmt->listJoin('timestamp'), $condition);
				$recordSet = $this->db->limit($sqlTotal, $limitResources, 0);
			}
			else
				$recordSet = $this->db->select(array("WKX_resource"),  
					$stmt->listFields('timestamp'), 
					$stmt->listJoin('timestamp', $condition));
		}
		else // Display set number
		{
			$sqlTotal = $this->db->selectNoExecute(array("WKX_resource"),  
				$stmt->listFields('timestamp'), 
				$stmt->listJoin('timestamp'));
			$recordSet = $this->db->limit($sqlTotal, $limit, 0);
		}
		if(!$this->db->numRows($recordSet))
			return FALSE;
		$index = 0;
		$pString = MISC::br() . "&nbsp;" . MISC::br() . 
			MISC::h($this->messages->text("resources", "lastChanges"), 4);
		$pString .= TABLE::tableStart(FALSE, 0, 0, 0, "left");
		$stats->getMaxAccesses();
		while($row = $this->db->loopRecordSet($recordSet))
		{
			$info = $edit = FALSE;
			$links = array();
// display CMS link if required
// link is actually a JavaScript call
			if($this->session->getVar('setup_displayCmsLink'))
				$links[] = MISC::a('cmsLink', "CMS:&nbsp;" . $row['resourceId'], 
					"javascript:openCmsWindow('index.php?action=cmsDisplay" . "&amp;id=" . $row['resourceId'] . "')");
// display bibtex link if required
// link is actually a JavaScript call
			if($this->session->getVar('setup_displayBibtexLink'))
				$links[] = MISC::a($bibtexLink, $icons->bibtex, 
					"javascript:openBibtexWindow('index.php?action=bibtexDisplay" . "&amp;id=" . $row['resourceId'] . "')");
			if($write && !$WIKINDX_ORIGINATOR_EDITONLY)
			{
				$links[] = MISC::a($editLink, $icons->edit, 
				"index.php?action=resourceEdit" . htmlentities("&id=" . $row['resourceId']));
				$edit = TRUE;
			}
			else if($write && ($row['addUserIdResource'] == $userId))
			{
				$links[] = MISC::a($editLink, $icons->edit, 
				"index.php?action=resourceEdit" . htmlentities("&id=" . $row['resourceId']));
				$edit = TRUE;
			}
			if($multiUser)
			{
				$userAddEdit = $user->displayUserAddEdit($row['addUserIdResource'], $row['editUserIdResource']);
// tidy up $userAddEdit
				$userArray = explode("&nbsp;&nbsp;&nbsp;", $userAddEdit);
				if(array_key_exists(1, $userArray))
					$info = trim(preg_replace("/&nbsp;+/", ' ', $userArray[1]));
				else
					$info = str_replace("<br />", '', $userArray[0]);
				$info .= MISC::br();
			}
			$timeStamp = MISC::span(str_replace(" ", "&nbsp;", $row['timestamp']), 'hint');
			$info .= $timeStamp;
			if($row['quotes'] || $row['paraphrases'])
				$view = $icons->viewmeta;
			else if($row['musings'])
			{
				$view = FALSE;
				$musStmt = $this->db->select(array('WKX_resource_musing'), 
					array('musingPrivate', 'addUserIdMusing'), 
					" LEFT JOIN " . $this->db->formatTable('WKX_resource_musing_text') . 
					" ON " . $this->db->formatField('WKX_resource_musing.id') . '=' . 
					$this->db->formatField('WKX_resource_musing_text.id') . 
					" WHERE " . $this->db->formatField('WKX_resource_musing.resourceId') . '=' . 
					$this->db->tidyInput($row['resourceId']));
				while($rowMus = $this->db->loopRecordSet($musStmt))
				{
// Public musings -- display available meta
					if($rowMus['musingPrivate'] == 'N')
						$view = $icons->viewmeta;
// logged on user and own musing
					else if($userId == $rowMus['addUserIdMusing'])
						$view = $icons->viewmeta;
					if($view)
						break;
				}
				if(!$view)
					$view = $icons->view; // default
			}
			else
				$view = $icons->view; // default
			$links[] = MISC::a($viewLink, $view, "index.php?action=resourceView" . 
				htmlentities("&id=" . $row['resourceId']));
			if($superAdmin)
			{
				if(!$edit)
					$links[] = MISC::a($editLink, $icons->edit, 
					"index.php?action=resourceEdit" . htmlentities("&id=" . $row['resourceId']));
				$links[] = MISC::a($deleteLink, $icons->delete, "index.php?action=deleteResourceConfirm" . 
					htmlentities("&resource_id=" . $row['resourceId']));
			}
			$link = FALSE;
			foreach($links as $item)
				$link .= $item . "&nbsp;";
			$class = $index % 2 ? 'alternate2' : 'alternate1';
			if($multiUser)
			{
				$popularityIndex = "&nbsp;" . $this->messages->text("misc", "popIndex") . "&nbsp;" . 
					$stats->getPopularityIndex($row['accesses']) . "%";
				$maturityIndex = $row['maturityIndex'] ? "&nbsp;" . 
					$this->messages->text("misc", "matIndex") . "&nbsp;" . 
					$row['maturityIndex'] . "/10" . MISC::br()
					:
					FALSE;
			}
			$pString .= TABLE::trStart();
// write Coins <span> data
			$coinsString = $coins->coinsDisplay($row);
			$pString .= TABLE::td($coinsString . $bibStyle->process($row), $class);
			$pString .= TABLE::td("&nbsp;&nbsp;", $class, "left", "top");
			$pString .= TABLE::td($info, $class, "left", "top");
			$pString .= TABLE::td("&nbsp;&nbsp;", $class, "left", "top");
			$pString .= TABLE::td($link, $class, "right", "top");
			if($multiUser)
				$pString .= TABLE::td(MISC::span($maturityIndex . $popularityIndex, 'hint'), 
					$class, "right", "top");
			$pString .= TABLE::trEnd();
			$allIds[] = $row['resourceId'];
			$index++;
		}
		if(isset($allIds))
			$this->session->setVar('list_allIds', base64_encode(serialize($allIds)));
		else
			$this->session->delVar("list_allIds");
		$pString .= TABLE::tableEnd();
		return $pString;
	}
}
?>
